<?
include("../mysql/database_info.php");
include("../common.php");
include("../mysql/common.php");
connect_select_test("churchMembershipFormReplies"); //placeholder
if ($_POST['questions'] && $_POST['answers'] && $_POST['id']){
	$questions = explode("|Q|", $_POST['questions']);
	$answers = explode("|A|", $_POST['answers']);
	$name = explode("<br>", $answers[0])[0];
	
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	$to = "eldership@southhillcalvary.org, whitneys@southhillcalvary.org";
	$subject = "New Church Membership Questionnaire";
	for ($x=0;$x<count($questions);$x++){
		$formReply .= "<b>" . $questions[$x] . "</b><br>" . nl2br($answers[$x]) . "<br><br>";
	}
	$message = $name . " finished their church membership questionnaire. To view this questionnaire please visit the following page:<br><br><a href='http://www.southhillcalvary.org/churchMembership/index.php'>Church Membership Questionnaire Manager</a><br><br>If you need the password please contact <a href='mailto:webservant@southhillcalvary.org'>the webservant</a>.";
	if ($_POST['sendMail'] == "1"){
		$valid[] = mail($to, $subject, $message, $headers);
	} else {
		$valid[] = 1;
	}
	$valid[] = mysql_query("UPDATE `churchMembershipFormReplies` SET `name`='".mysql_real_escape_string($name)."', `data`='".mysql_real_escape_string($formReply)."', `date`='".date("Y-m-d")."', `read`='new', `complete`='".$_POST['sendMail']."' WHERE `id`='".$_POST['id']."'");
	if ($valid[0] && $valid[1]){
		echo "success";
	} else {
		echo "error";
		echo print_r($valid);
		die();
	}
} else {
	echo "invalid link";
}
?>